2020-2021 Sunseeker Telemetry and Lighting System
timer_d_ex9_periodMeasurement.c
Go to the documentation of this file.
1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2017, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * --/COPYRIGHT--*/
32 //******************************************************************************
33 // MSP430F51x2 Demo - TimerD0, Dual Input capture mode, Normal timer mode,
34 // Period measurement
35 // Description: This code example implements input capture in dual capture
36 // mode using TimerD in normal timer mode. Dual capture mode of TimerD is
37 // demonstrated here. This example captures two consequent rising edges and
38 // computes the Period of the input signal. The TD0.0 input signal is generated
39 // by the Timer A0 module (TA0.0) and the freq of this PWM input signal can be
40 // changed by modifying the TA0CCR0 register.
41 //
42 // ACLK = LFXT1 = 32kHz; SMCLK = MCLK = 12MHz
43 //
44 // MSP430F51x2
45 // -----------------
46 // /|\| XIN|-
47 // | | | 32kHz
48 // --|RST XOUT|-
49 // | |
50 // | P1.6/TD0.0|<-- CCI0A <-|
51 // | P3.7/TA0.0|--> CCR0 -->|
52 // | |
53 // | P3.0|--> MCLK = 12MHz DCO
54 // | P3.1|--> SMCLK = 12MHz DCO
55 // | P3.2|--> ACLK = 32kHz LFXT1
56 //
57 //******************************************************************************
58 #include "driverlib.h"
59 
60 uint16_t REdge1, REdge2, Period;
61 
62 const uint8_t port_mapping[] = {
63  //Port P3:
64  PM_TD0CLKMCLK,
65  PM_TD0_0SMCLK,
66  PM_TD1OUTH,
67  PM_NONE,
68  PM_NONE,
69  PM_NONE,
70  PM_NONE,
71  PM_TA0_0,
72  };
73 
74 
75 void main(void)
76 {
77  // Stop WDT
78  WDT_A_hold(WDT_A_BASE);
79 
80  // Configure XT1
81  GPIO_setAsPeripheralModuleFunctionInputPin(
82  GPIO_PORT_PJ,
83  GPIO_PIN4 + GPIO_PIN5
84  );
85 
86  UCS_turnOnLFXT1 (UCS_XT1_DRIVE_3,
87  UCS_XCAP_3
88  );
89 
90 
91  // Increase Vcore setting to level1 to support fsystem=12MHz
92  // NOTE: Change core voltage one level at a time..
93  PMM_setVCore (PMM_CORE_LEVEL_1);
94 
95  // Initialize DCO to 12MHz
96  UCS_initFLLSettle(12000,
97  374);
98 
99 
100  // Setup Port Pins
101  // P3.0 - P3.2 output
102  // Port map P3.0 - P3.2
103  GPIO_setAsPeripheralModuleFunctionOutputPin(
104  GPIO_PORT_P3,
105  GPIO_PIN0 + GPIO_PIN1 + GPIO_PIN2
106  );
107 
108  PMAP_initPortsParam initPortsParam = {0};
109  initPortsParam.portMapping = (const uint8_t *)port_mapping;
110  initPortsParam.PxMAPy = (uint8_t *)&P3MAP0;
111  initPortsParam.numberOfPorts = 1;
112  initPortsParam.portMapReconfigure = PMAP_DISABLE_RECONFIGURATION;
113  PMAP_initPorts(PMAP_CTRL_BASE, &initPortsParam);
114 
115 
116  // Configure ports TD0.0 input and TA0.0 output
117  // TD0.0 input
118  // TD0.0 option select
119  // TA0.0 output
120  // TA0.0 option select
121  GPIO_setAsPeripheralModuleFunctionInputPin(
122  GPIO_PORT_P1,
123  GPIO_PIN6
124  );
125 
126  GPIO_setAsPeripheralModuleFunctionOutputPin(
127  GPIO_PORT_P3,
128  GPIO_PIN7
129  );
130 
131  // Configure LED on P1.0, output and driving low
133  GPIO_PORT_P1,
134  GPIO_PIN0
135  );
136  GPIO_setAsOutputPin(
137  GPIO_PORT_P1,
138  GPIO_PIN0
139  );
140 
141  // Configure TA0.0 compare output, 1kHz freq, 50% dutycycle
142  Timer_A_initCompareModeParam initCompParam = {0};
143  initCompParam.compareRegister = TIMER_A_CAPTURECOMPARE_REGISTER_0;
144  initCompParam.compareInterruptEnable = TIMER_A_CAPTURECOMPARE_INTERRUPT_DISABLE;
145  initCompParam.compareOutputMode = TIMER_A_OUTPUTMODE_TOGGLE;
146  initCompParam.compareValue = 32;
147  Timer_A_initCompareMode(TIMER_A0_BASE, &initCompParam);
148 
149  Timer_A_initUpModeParam initUpParam = {0};
150  initUpParam.clockSource = TIMER_A_CLOCKSOURCE_ACLK;
151  initUpParam.clockSourceDivider = TIMER_A_CLOCKSOURCE_DIVIDER_1;
152  initUpParam.timerPeriod = 32;
153  initUpParam.timerInterruptEnable_TAIE = TIMER_A_TAIE_INTERRUPT_DISABLE;
154  initUpParam.captureCompareInterruptEnable_CCR0_CCIE =
155  TIMER_A_CAPTURECOMPARE_INTERRUPT_DISABLE;
156  initUpParam.timerClear = TIMER_A_DO_CLEAR;
157  initUpParam.startTimer = false;
158  Timer_A_initUpMode(TIMER_A0_BASE, &initUpParam);
159 
160  Timer_A_startCounter(TIMER_A0_BASE,
161  TIMER_A_UP_MODE
162  );
163 
164  Timer_D_initContinuousModeParam initContParam = {0};
165  initContParam.clockSource = TIMER_D_CLOCKSOURCE_SMCLK;
166  initContParam.clockSourceDivider = TIMER_D_CLOCKSOURCE_DIVIDER_1;
167  initContParam.clockingMode = TIMER_D_CLOCKINGMODE_EXTERNAL_CLOCK;
168  initContParam.timerInterruptEnable_TDIE = TIMER_D_TDIE_INTERRUPT_DISABLE;
169  initContParam.timerClear = TIMER_D_DO_CLEAR;
170  Timer_D_initContinuousMode(TIMER_D0_BASE, &initContParam);
171 
172  Timer_D_startCounter(TIMER_D0_BASE,
173  TIMER_D_CONTINUOUS_MODE
174  );
175 
176  Timer_D_clearCaptureCompareInterrupt(TIMER_D0_BASE,
177  TIMER_D_CAPTURECOMPARE_REGISTER_0);
178 
179  Timer_D_initCaptureModeParam initCapParam = {0};
180  initCapParam.captureRegister = TIMER_D_CAPTURECOMPARE_REGISTER_0;
181  initCapParam.captureMode = TIMER_D_CAPTUREMODE_RISING_EDGE;
182  initCapParam.captureInputSelect = TIMER_D_CAPTURE_INPUTSELECT_CCIxA;
183  initCapParam.synchronizeCaptureSource = TIMER_D_CAPTURE_SYNCHRONOUS;
184  initCapParam.captureInterruptEnable = TIMER_D_CAPTURECOMPARE_INTERRUPT_ENABLE;
185  initCapParam.captureOutputMode = TIMER_D_OUTPUTMODE_OUTBITVALUE;
186  initCapParam.channelCaptureMode = TIMER_D_DUAL_CAPTURE_MODE;
187  Timer_D_initCaptureMode(TIMER_D0_BASE, &initCapParam);
188 
189  while(1)
190  {
191  __bis_SR_register(LPM0_bits + GIE); // Enter LPM0
192  __no_operation(); // For debugger
193  // On exiting LPM0
194  if (TIMER_D_CAPTURE_OVERFLOW == Timer_D_getCaptureCompareInterruptStatus
195  (TIMER_D0_BASE,
196  TIMER_D_CAPTURECOMPARE_REGISTER_1,
197  TIMER_D_CAPTURE_OVERFLOW
198  ) ) // Check for Capture Overflow
199  while(1); // Loop Forever
200 
201  Period = REdge2-REdge1; // Measured Period
202  __no_operation(); // BREAKPOINT HERE - measured period ~ 2ms
203  // Period ~ 2m*TD0 clock counts
204  }
205 }
206 
207 // Timer0_D0 Interrupt Vector
208 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
209 #pragma vector=TIMER0_D0_VECTOR
210 __interrupt
211 #elif defined(__GNUC__)
212 __attribute__((interrupt(TIMER0_D0_VECTOR)))
213 #endif
214 void TIMER0_D0_ISR (void)
215 {
216  REdge1 = Timer_D_getCaptureCompareLatchCount(TIMER_D0_BASE,
217  TIMER_D_CAPTURECOMPARE_REGISTER_0);
218 
219  REdge2 = Timer_D_getCaptureCompareCount(TIMER_D0_BASE,
220  TIMER_D_CAPTURECOMPARE_REGISTER_0);
221  __bic_SR_register_on_exit(LPM0_bits); // Exit LPM0
222 }
223 
224 
__no_operation()
__bic_SR_register_on_exit(LPM3_bits|GIE)
GPIO_setOutputLowOnPin(GPIO_PORT_LED1|GPIO_PORT_LED2, GPIO_PIN_LED1|GPIO_PIN_LED2)
void TIMER0_D0_ISR(void)
void main(void)
uint16_t REdge2
uint16_t Period
uint16_t REdge1
const uint8_t port_mapping[]